package com.tianqu.tms.www.assess.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferPersonalCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.assess.actionform.AssessSerchInputForm;
import com.tianqu.tms.www.assess.viewhelper.AssessCarInfoInputViewHelper;

/**
 * 处理流水号查询结果的跳转
 * 
 * @author Administrator
 * 
 */
public class CarAssessSerachDispatchAction extends TmsAction {
	private Logger logger = Logger
			.getLogger(CarAssessSerachDispatchAction.class);

	private static final String NOTUPDATEVIN = "notUpdateVin";

	private static final String NOTASSESSINPUT = "notAssessInput";

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		AssessSerchInputForm inputForm = (AssessSerchInputForm) form;
		String serial = inputForm.getSerial();
		AssessCarInfoInputViewHelper view = new AssessCarInfoInputViewHelper();
		Codes code = Codes.getInstance();
		Car car = new Car();

		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.getAssessInfoBySerial(serial);
		String actionForward = "";

		if (accept != null) {
			//workload:3.1.2
			if (accept.getStatus() == 3 && accept.getOwnerType() == 2 && (accept.getSourceType() == 1 )) {
				view.setSerial(serial);
				view.setLicensePlate(accept.getLicensePlate());
				view.setVin(accept.getVin());
				view.setBasicCarId(accept.getBasicCarId());
				view.setTransStatus(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS, accept.getStatus()));
				view.setMarketType(accept.getMarketType());

				/** history record **/
				TransferProcessHistory his = new TransferProcessHistory();
				List list = his.loadtHistoryBySerial(serial);
				view.setLcList(list);
				if(accept.getMarketType() == 1)
				{
					car = car.findCarBySerial(accept.getBasicCarId());
					view = setViewFromEntity(view, car);
				}
				if(accept.getMarketType() == 2)
				{
					TransferPersonalCarInfo person = new TransferPersonalCarInfo();
					person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
					view = setViewFromEntity1(view, person);
				}
				if (accept.getVin() != null && !"".equals(accept.getVin())) {
					TransferAcceptedInfo accept1 = new TransferAcceptedInfo();
					accept1 = accept1.searchLastAssessInfoByVin(accept.getVin());
					if (accept1 != null) {
						if (accept1.getPg_price() > 0) {
							view.setLastPgPrice(StringUtil.addDH(String.valueOf(accept1.getPg_price())));
						} else {
							view.setLastPgPrice("");
						}
						if(accept1.getPg_time() == null || "".equals(accept1.getPg_time()))
						{
							view.setLastPgTime("");
						}else{
							view.setLastPgTime(accept1.getPg_time());
						}
						
					} else {
						view.setLastPgPrice("");
						view.setLastPgTime("");
					}
					uc.setAttribute("backBsflag", "1");
					actionForward = NOTUPDATEVIN;
				} else {
					view.setLastPgPrice("");
					view.setLastPgTime("");
					uc.setAttribute("backBsflag", "2");
					actionForward = NOTASSESSINPUT;
				}
			} else {
				setErrorsToRequest(request, TmsConstants.IM006, null);
				return mapping.getInputForward();
			}
		} else {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

	/**
	 * car -> view
	 */
	private AssessCarInfoInputViewHelper setViewFromEntity(
			AssessCarInfoInputViewHelper view, Car car) {
		view.setVendorCode(car.getVendorCode());
		view.setModelCode(car.getModelCode());
		view.setCarType(String.valueOf(car.getCarType()));
		view.setCarUse(String.valueOf(car.getCarUse()));
		view.setColor(String.valueOf(car.getColor()));
		view.setCarType(String.valueOf(car.getCarType()));
		view.setRegDate(car.getRegDate());
		view.setSendCardDate(car.getSendCardDate());
		view.setCarOwner(car.getCarOwner());
		return view;
	}
	private AssessCarInfoInputViewHelper setViewFromEntity1(
			AssessCarInfoInputViewHelper view, TransferPersonalCarInfo car) {
		view.setVendorCode(car.getVendorCode());
		view.setModelCode(car.getModelCode());
		view.setCarType(String.valueOf(car.getCarType()));
		view.setCarUse(String.valueOf(car.getCarUse()));
		view.setColor(String.valueOf(car.getColor()));
		view.setCarType(String.valueOf(car.getCarType()));
		view.setRegDate(car.getRegDate());
		view.setSendCardDate(car.getSendCardDate());
		view.setCarOwner(car.getCarOwner());
		return view;
	}

}
